package mmi.gui.shape;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;

/**
 * Ellipse is a wrapper for the Java provided Rectangle but with a closed public interface
 * 
 * @author louis
 *
 */
public class Ellipse extends Shape {

	public Ellipse(Point2D start, Point2D end, Color lineColour, Color fillColour, int lineWidth) {
		super(start, end, lineColour, fillColour, lineWidth);
		
		makeTheShape();
	}
	
	public Ellipse(int id, Point2D start, Point2D end, Color lineColour, Color fillColour, int lineWidth) {
		super(id, start, end, lineColour, fillColour, lineWidth);
		
		makeTheShape();
	}

	@Override
	public void draw(Graphics2D g) {
		Stroke defaultStroke = g.getStroke();
		Color defaultColour = g.getColor();
		
		g.setStroke(new BasicStroke(lineWidth, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER));
		g.setColor(lineColour);
		
		g.draw(ellipse);
		
		g.setColor(fillColour);
		
		fill(g);
		
		g.setStroke(defaultStroke);
		g.setColor(defaultColour);
		
		drawBoundingBox(g);
	}

	@Override
	public void fill(Graphics2D g) {
		Color defColour = g.getColor();
		if(fillColour == null)
			g.setColor(Color.white);
		else
			g.setColor(fillColour);
		g.fill(ellipse);
		g.setColor(defColour);
	}

	Ellipse2D ellipse = null;

	/*
	 * The contain member function of an Ellipse2D doesn't keep track of our bounding boxes.
	 * If we click on a bounding box of a shape, that shape should say it contains that point,
	 * so that we can resize the shape. 
	 */
	@Override
	public boolean contains(Point2D position) {
		return isSide(position) != null || ellipse.contains(position);
	}

	@Override
	public String getXMLRepresentation() {
		String s = "";
		
		s += "\t\t<Shape shapetype=\"Ellipse\" id=\"" + id + "\">" + "\n";
		s += "\t\t\t<xstart>" + start.getX() + "</xstart>" + "\n";
		s += "\t\t\t<ystart>" + start.getY() + "</ystart>" + "\n";
		s += "\t\t\t<xend>" + end.getX() + "</xend>" + "\n";
		s += "\t\t\t<yend>" + end.getY() + "</yend>" + "\n";
		s += "\t\t\t<linecolour>" + lineColour + "</linecolour>" + "\n";
		s += "\t\t\t<fillcolour>" + fillColour + "</fillcolour>" + "\n";
		s += "\t\t\t<linewidth>" + lineWidth + "</linewidth>" + "\n";
		s += "\t\t</Shape>";

		return s;
	}
	
	@Override
	public String toString() {
		String s = "";
		
		s += "Ellipse [" + id + "]: " + start.getX() + " " + start.getY() + " " + end.getX() + " " + end.getY();
		
		return s;
	}

	@Override
	public Shapes getShape() {
		return Shapes.Ellipse;
	}
	
	@Override
	public void makeTheShape()
	{
		if(start.getX() <= end.getX() && start.getY() <= end.getY()) // top left to bottom right
		{
			ellipse = new Ellipse2D.Double(start.getX(), start.getY(), end.getX() - start.getX(), end.getY() - start.getY());
		} else if(start.getX() <= end.getX() && start.getY() >= end.getY()) // bottom left to top right
		{
			ellipse = new Ellipse2D.Double(start.getX(), end.getY(), end.getX() - start.getX(), start.getY() - end.getY());
		} else if(start.getX() >= end.getX() && start.getY() >= end.getY()) // bottom right to top left
		{
			ellipse = new Ellipse2D.Double(end.getX(), end.getY(), start.getX() - end.getX(), start.getY() - end.getY());
		} else if(start.getX() >= end.getX() && start.getY() <= end.getY()) // top right to bottom left
		{
			ellipse = new Ellipse2D.Double(end.getX(), start.getY(), start.getX() - end.getX(), end.getY() - start.getY());
		}
	}
}
